package com.lcq.demo;

import org.junit.Test;

import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName WordTemplateTest
 * @Description
 * @Author lichuanqi
 * @Date 2022/3/15 11:49
 * @Version 1.0
 **/
public class WordTemplateTest {

    public static void main(String[] args) throws IOException {

        Map<String, Object> wordDataMap = new HashMap<String, Object>();// 存储报表全部数据
        Map<String, Object> parametersMap = new HashMap<String, Object>();// 存储报表中不循环的数据



        List<Map<String, Object>> table1 = new ArrayList<Map<String, Object>>();
        Map<String, Object> map1=new HashMap<>();
        map1.put("name", "张三");
        map1.put("age", "23");
        map1.put("email", "12121@qq.com");

        Map<String, Object> map2=new HashMap<>();
        map2.put("name", "李四");
        map2.put("age", "45");
        map2.put("email", "45445@qq.com");

        Map<String, Object> map3=new HashMap<>();
        map3.put("name", "Tom");
        map3.put("age", "34");
        map3.put("email", "6767@qq.com");

        table1.add(map1);
        table1.add(map2);
        table1.add(map3);




        List<Map<String, Object>> table2 = new ArrayList<Map<String, Object>>();
        Map<String, Object> map4=new HashMap<>();
        map4.put("name", "tom");
        map4.put("number", "sd1234");
        map4.put("address", "上海");

        Map<String, Object> map5=new HashMap<>();
        map5.put("name", "seven");
        map5.put("number", "sd15678");
        map5.put("address", "北京");

        Map<String, Object> map6=new HashMap<>();
        map6.put("name", "lisa");
        map6.put("number", "sd9078");
        map6.put("address", "广州");

        table2.add(map4);
        table2.add(map5);
        table2.add(map6);



        parametersMap.put("userName", "JUVENILESS");
        parametersMap.put("time", "2018-03-24");
        parametersMap.put("sum", "3");


        wordDataMap.put("table1", table1);
        wordDataMap.put("table2", table2);
        wordDataMap.put("parametersMap", parametersMap);
        File file = new File("E:\\test\\模板.docx");//改成你本地文件所在目录


        // 读取word模板
        FileInputStream fileInputStream = new FileInputStream(file);
        WordTemplate template = new WordTemplate(fileInputStream);

        // 替换数据
        template.replaceDocument(wordDataMap);


        //生成文件
        File outputFile=new File("E:\\test\\输出.docx");//改成你本地文件所在目录
        FileOutputStream fos  = new FileOutputStream(outputFile);
        template.getDocument().write(fos);

    }

    @Test
    public void test() throws IOException {
        Map<String, Object> paraMap = new HashMap<>();
        Map<String, Object> wordDataMap = new HashMap<>();
        paraMap.put("name", "张三");
        paraMap.put("age", "25");
        paraMap.put("email", "zhangsan@bimex.com");
        paraMap.put("spec", "□总图  ■建筑  □结构  □电气  □给排水  □通风空调  □燃气  □装配式混凝土结构  □钢结构  □智能化");
        wordDataMap.put("parametersMap", paraMap);

        List<Map<String, Object>> table1List = new ArrayList<>();
        Map<String, Object> row1Map = new HashMap<>();
        row1Map.put("goodName", "笔记本电脑");
        row1Map.put("goodNum", "aaa-bbb-ccc");
        row1Map.put("goodPrice", 4999.00);
        row1Map.put("goodStock", 1000);
        table1List.add(row1Map);

        Map<String, Object> row2Map = new HashMap<>();
        row2Map.put("goodName", "手机");
        row2Map.put("goodNum", "ddd-eee-fff");
        row2Map.put("goodPrice", 3899.00);
        row2Map.put("goodStock", 666);
        table1List.add(row2Map);

        Map<String, Object> row3Map = new HashMap<>();
        row3Map.put("goodName", "游戏机");
        row3Map.put("goodNum", "111-222-333");
        row3Map.put("goodPrice", 1599.00);
        row3Map.put("goodStock", 321);
        table1List.add(row3Map);

        Map<String, Object> table1Map = new HashMap<>();
        table1Map.put("list", table1List);
        table1Map.put("avgPrice", (4999.00+3899.00+1599.00)/3);
        table1Map.put("avgStock", (1000+666+321)/3);


        List<Map<String, Object>> table2List = new ArrayList<>();
        Map<String, Object> row1Map2 = new HashMap<>();
        row1Map2.put("goodName", "笔记本电脑");
        row1Map2.put("goodNum", "aaa-bbb-ccc");
        row1Map2.put("goodPrice", 4999.00);
        row1Map2.put("goodStock", 1000);
        table2List.add(row1Map2);

        Map<String, Object> row2Map2 = new HashMap<>();
        row2Map2.put("goodName", "手机");
        row2Map2.put("goodNum", "ddd-eee-fff");
        row2Map2.put("goodPrice", 3899.00);
        row2Map2.put("goodStock", 666);
        table2List.add(row2Map2);

        Map<String, Object> row3Map2 = new HashMap<>();
        row3Map2.put("goodName", "游戏机");
        row3Map2.put("goodNum", "111-222-333");
        row3Map2.put("goodPrice", 1599.00);
        row3Map2.put("goodStock", 321);
        table2List.add(row3Map2);

        Map<String, Object> table2Map = new HashMap<>();
        table2Map.put("list", table2List);

        wordDataMap.put("table1", table1Map);
        wordDataMap.put("table2", table2Map);

        File inputFile = new File("E:/test/模板1.docx");
        File outputFile = new File("E:/test/模板1out.docx");
        // 读取word模板
        FileInputStream fileInputStream = new FileInputStream(inputFile);
        WordTemplate template = new WordTemplate(fileInputStream);

        // 替换数据
        template.replaceDocument(wordDataMap);

        FileOutputStream fos  = new FileOutputStream(outputFile);
        template.getDocument().write(fos);
    }
}
